<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Buyer;
use App\Models\BuyerTask;
use Auth;
use Carbon\Carbon;

class UserController extends Controller
{
    public function info(Request $request)
    {
    	
       
        $rs['status'] = false;
    	if(Auth::user()->buyer) {
    		$rs['status'] = true;  
            $buyer_id     = Auth::user()->buyer->id;
    		$user         = Buyer::find($buyer_id)->toArray();
            $yesterday = Carbon::yesterday()->toDateString();
            $money = BuyerTask::where('buyer_id',$buyer_id)
                                ->whereDate('updated_at', $yesterday)
                                ->sum('commission');
            $user['yesterday_money'] = $money;
            $user['money'] = $user['money']-$user['freeze_money'];              
            $rs['data'] = $user;
            return response()->json($rs);	
    	}
    	$rs['msg'] = '获取用户信息失败';
    	return response()->json($rs); 
    }

    
    /**
     * 保存收款账号
     */
    public function saveAccount(Request $request)
    {
        $rs['status'] = false;
        if(!$request->alipay && !$request->wechatpay) {
            $rs['msg'] = '支付宝或者微信账号不能为空';
            return reponse()->json($rs);
        }
        $buyer_id     = Auth::user()->buyer->id;
        $buyer        = Buyer::find($buyer_id); 
        if ($request->alipay) {
            $buyer->alipay = $request->alipay;    
        }

        if ($request->wechatpay) {
            $buyer->wechatpay = $request->wechatpay;    
        }
        
        
        $flag = $buyer->save();
        if ($flag) {
            $rs['status'] = true;
            return response()->json($rs);
        }   

        $rs['msg'] = '操作失败';
        return response()->json($rs); 
    }

   
}
